<?php
namespace Common\Job;

class SellerTaskJob
{
    public function setUp()
    {
        $data = $this->args;
        // ... Set up environment for this job
        fwrite(STDOUT, $data['store_id'].'开始执行' . PHP_EOL);
    }

    public function perform()
    {
        $data = $this->args;

        fwrite(STDOUT, $data['store_id'].'('.date('Y-m-d H:i:s',time()).')正在执行' . PHP_EOL);

        $store_id = $data['store_id'];
        //批量修改商品规格属性最低价格&最新库存。
        // $goods=M('goods a')
        //     ->field('goods_id,prom_id,prom_lock,zd,prom_type')
        //     ->where('store_id='.$store_id.' and is_on_sale=1')->select(); sofu
		$goods=M('goods a')
		->field('a.goods_id,a.prom_id,a.prom_lock,a.prom_type,b.start_time,b.end_time,b.expression')
		->join('ty_prom_goods b on a.prom_id=b.id','LEFT')
		->where('a.store_id='.$store_id)->select();
        $prom_zd_id = M('prom_goods')->where(['store_id'=>$store_id,'zd'=>1])->where('start_time < '.time().' and end_time > '.time().' ')->getField('id');

        foreach ($goods as $v) {

            $minprice = M('spec_goods_price')->where('goods_id=' . $v['goods_id'])->order('price','ASC')->getField('price');
            $kucun = M('spec_goods_price')->where('goods_id=' . $v['goods_id'])->sum('store_count');

            //$prom = M('prom_goods')->where(['id'=>$v['prom_id']])->find(); sofu

            if ($minprice) {
                //删除过期活动
                if ($v['prom_id'] != 0 && $v['end_time'] < time()) {
                    //已经结束，初始化产品
                    $data = [
                        "shop_price" => $minprice,
                        "store_count" => $kucun,
                        "prom_type" => 0,
                        "prom_id" => 0,
                        "zd" => 0,
                    ];
                // }elseif($v['prom_id'] != 0 && $v['start_time'] > time() ){
                }else{
                    //活动还没有开始
                    $data = [
                        "shop_price" => $minprice,
                        "store_count" => $kucun,
                    ];
                }
                // else {
                //      if($v['prom_id'] > 0){
                //          if ($v['type'] == 0){
                //              $prom_goods_price = geshi($minprice * ($v['expression'] / 100)); //活动折扣
                //          }elseif($v['type'] == 1){
                //              $prom_goods_price = $minprice - $v['expression'];
                //          }elseif($v['type'] == 2){
                //              $prom_goods_price = $v['expression'];
                //          }elseif($v['type'] == 5){
                //              $prom_goods_price = geshi($minprice * ($v['expression'] / 100)); //活动折扣
                //          }
                //      }else{
                //          $prom_goods_price = $minprice;
                //      }
                //      $data = [
                //          "shop_price" => $prom_goods_price,
                //          "store_count" => $kucun,
                //          "xxx" => 2
                //      ];
                //  }


                M('goods')->where('goods_id='.$v['goods_id'])->setField($data);

                if($v['prom_lock'] == 1 && $v['zd'] == 1 && $v['prom_type'] == 0){
                    M('goods')->where('goods_id='.$v['goods_id'])->where(['prom_lock'=>1])->setField(['prom_id'=>0,'zd'=>0,'prom_type'=>0]);
                }

                if($prom_zd_id){
                    M('goods')->where('goods_id='.$v['goods_id'])->where(['prom_id'=>0,'prom_lock'=>0,'prom_type'=>0])->setField(['prom_id'=>$prom_zd_id,'zd'=>1]);
                }
            }
        }


        fwrite(STDOUT, $store_id.'执行完成' . PHP_EOL);
    }

    public function tearDown()
    {
        $data = $this->args;
        // ... Set up environment for this job
        fwrite(STDOUT, $data['store_id'].'结束执行' . PHP_EOL);
    }
}